package com.bytedance.improvingroad.widget

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.bytedance.improvingroad.utils.Utils

/**
 * Created by chenxiong
 * date 1/9/22
 */
class TestDrawView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, style: Int = 0): View(
    context,attr,style
) {
    init {
        setBackgroundColor(Color.CYAN)
    }
    private var textView: TextView = TextView(context).apply {
        layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        text = "faklfjadsklafjlkafjdsalkdjfladfjsla"
        setTextColor(Color.BLACK)
    }
    private val bitmap: Bitmap by lazy {
        textView.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        textView.layout(0,0,textView.measuredWidth,textView.measuredHeight)
        Bitmap.createBitmap(textView.measuredWidth, textView.measuredHeight, Bitmap.Config.ARGB_8888)
    }

    private val rect: Rect by lazy {
        Rect(0,0,textView.right,textView.bottom)
    }

    private val paint = Paint().apply {
        color = Color.RED
        strokeWidth = 20f
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(Utils.dp2px(100),Utils.dp2px(100))
    }

    override fun onDraw(canvas: Canvas?) {
        canvas?.drawBitmap(bitmap, 0f,0f,paint)
        canvas?.drawLine(0f,0f,50f,50f,paint)
    }
}